package com.niiwoo.civet.trade.service.local.notify.impl;

import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestOrderMapperExt;
import com.niiwoo.civet.trade.service.local.notify.AbstractNotifier;
import com.niiwoo.civet.user.enums.SMSTemplateKeyEnum;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractInvestNotifier<T extends Project> extends AbstractNotifier<T> {


    @Autowired
    ProjectInvestOrderMapperExt projectInvestOrderMapperExt;

    @Override
    protected List<? extends UserNotifyInfo> getUserNotifyInfoList(T t) {
        return Optional.ofNullable(projectInvestOrderMapperExt.selectInvestedList(t.getProjectId()))
                .filter(list -> !list.isEmpty())
                .map(list -> {
                    return list.stream()
                            .map(order -> {
                                InvestUserNotifyInfo info = new InvestUserNotifyInfo();
                                info.setProjectId(t.getProjectId());
                                info.setProjectTitle(t.getTitle());
                                info.setUserId(order.getUserId());
                                info.setOrderId(order.getId());
                                return info;
                            }).collect(Collectors.toList());
                })
                .orElse(Collections.emptyList());
    }

    @Override
    protected SMSRequest getSMSRequestDTO(UserNotifyInfo info) {
        return Optional.ofNullable(getSMSTemplateKey())
                .map(key -> {
                    InvestUserNotifyInfo investUserNotifyInfo = (InvestUserNotifyInfo) info;
                    SMSRequest request = new SMSRequest();
                    request.setUserId(investUserNotifyInfo.getUserId());
                    request.setSmsTemplateKeyEnum(key);
                    request.setParamList(this.getSMSParameterList(investUserNotifyInfo));
                    return request;
                })
                .orElse(null);
    }

    protected SMSTemplateKeyEnum getSMSTemplateKey() {
        return null;
    }

    protected List<Object> getSMSParameterList(InvestUserNotifyInfo notifyInfo) {
        return Collections.singletonList(notifyInfo.getProjectTitle());
    }

    @Getter
    @Setter
    protected static class InvestUserNotifyInfo extends UserNotifyInfo {

        private String userId;

        private Long projectId;

        private String projectTitle;

        private Long orderId;

    }

}
